<?php


namespace App\Admin\Controllers;


use App\Admin\Repositories\Product;
use App\Models\Brand;
use App\Models\Category as CategoryModel;
use App\Models\ProductTag;
use App\Models\Supplier;
use Cblink\Region\Region;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;

// 抽象商品控制器
abstract class CommonProductController extends AdminController
{
    // 定义一个抽象方法
    abstract protected function getProductType();

    // 自定义表格
    abstract protected function customGrid(Grid $grid);

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Product(['category']), function (Grid $grid) {
            $grid->model()->where('type', $this->getProductType());

            $grid->id->sortable();
            $grid->column('category.name', '所属分类');
            $grid->title;
            $grid->image->image('', '200');
            $grid->on_sale->display(function ($value) {
                return \App\Models\Product::$saleMap[$value];
            });
            $grid->rating;
            $grid->sold_count;
            $grid->review_count;
            $grid->price;
            $grid->long_title;
            $this->customGrid($grid);

            $grid->actions(function ($actions) {
                $actions->disableView();
                $actions->disableDelete();
            });

            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    // 自定义表单
    abstract protected function customForm(Form $form);

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Product(['skus', 'properties', 'productTags']), function (Form $form) {
            $form->tab('基本', function ($form) {

                $form->hidden('type')->default($this->getProductType());  // 商品类型
                $form->select('category_id', '类目')->options(function ($id) {
                    $category = CategoryModel::query()->find($id);
                    if ($category) {
                        return [ $category->id => $category->full_name ];
                    }
                })->ajax('/api/categories?is_directory=0')->required();
                $form->text('title')->rules('required')->required();  // 商品短标题
                $form->text('long_title')->rules('required')->required();  // 商品长标题
                $form->editor('description')->rules('required')->required();  // 商品详情
                $form->radio('on_sale')->options(\App\Models\Product::$saleMap)->default(0);  // 是否上架
                $form->multipleSelect('productTags')->options(ProductTag::all()->pluck('name', 'id')); // 商品标签

                // 省市区三级联动
//                $form->divider();
//                $region = new Region();
                $form->text('address');
//                $form->select('province')->options($region->allProvinces()->pluck('name', 'id'))->load('city', '/api/city');
//                $form->select('city')->load('area', '/api/area');
//                $form->select('area');
//                $form->divider();

                // 商品所属品牌
                $form->select('brand_id', '品牌')->options(Brand::all()->pluck('name', 'id'))->rules('required')->required();
                // 商品供应商
                $form->select('supplier_id', '供应商')->options(Supplier::all()->map(function ($supplier) {
                    $supplier->name = $supplier->name . " - " . $supplier->contact_name;
                    return $supplier;
                })->pluck('name', 'id'))->rules('required')->required();

                // 自定义表单
                $this->customForm($form);

            })->tab('图片详情', function ($form) {

                $form->image('image')->rules('required|image')->required();
                $form->multipleImage('banner')->rules('required|image')->required();

            })->tab('单品库存', function ($form) {

                $form->hasMany('skus', 'SKU 列表', function ($form) {
                    $form->text('title', 'SKU 名称')->rules('required');
                    $form->text('description', 'SKU 描述')->rules('required');
                    $form->text('price', 'SKU 价格')->rules('required|numeric|min:0.01');
                    $form->text('stock', '剩余库存')->rules('required|integer|min:0');
                });

            })->tab('商品属性', function ($form) {

                $form->hasMany('properties', '商品属性', function ($form) {
                    $form->text('name', '属性名称')->rules('required')->required();
                    $form->text('value', '属性内容')->rules('required')->required();
                });
                
            })->tab('虚拟数据', function ($form) {

                $form->number('base_view')->default(0)->rules('integer|min:0');
                $form->number('base_sale')->default(0)->rules('integer|min:0');
                $form->number('base_recommend')->default(0)->rules('integer|min:0');
                $form->number('base_share')->default(0)->rules('integer|min:0');

            });

            $form->saving(function (Form $form) {
                $form->price = collect($form->skus)->min('price') ?: 0;  // 商品最低价格
                $form->address = $form->province . $form->city . $form->area; // 商品生产地址
                $form->model()->productTags()->sync($form->productTags);  // 商品标签
            });
        });
    }
}
